package com.davy.gridpoint.activity;

import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.davy.gridpoint.BaseActivity;
import com.davy.gridpoint.R;
import com.davy.gridpoint.adapter.HomeAdapter;
import com.davy.gridpoint.bean.ChooseBean;
import com.davy.gridpoint.utils.SPUtils;
import com.davy.gridpoint.utils.StringUtils;
import com.davy.gridpoint.utils.ToastUtil;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class HomeActivity extends BaseActivity {
    private RecyclerView recyclerData;
    private HomeAdapter homeAdapter;
    private ArrayList<ChooseBean> stationList;
    private String strSelect, pwsSelect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        statusbar4();
        initView();
        initData();
        ka();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()) {
                delDialog();
            }
        }
    }

    private void ka() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()) {
                Intent appIntent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                appIntent.setData(Uri.parse("package:" + getPackageName()));
                try {
                    mContext.startActivity(appIntent);
                } catch (ActivityNotFoundException ex) {
                    ex.printStackTrace();
                    Intent allFileIntent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                    mContext.startActivity(allFileIntent);
                }
            }
        }
    }

    public void delDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.dialog_exit, null);
        TextView tvDelete = v.findViewById(R.id.tvDelete);
        TextView tvSave = v.findViewById(R.id.tvSave);
        TextView tvName = v.findViewById(R.id.tvName);
        tvDelete.setVisibility(View.GONE);
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);//点击屏幕不消失

        if (!dialog.isShowing()) {//此时提示框未显示
            dialog.show();
        }
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

        // 设置按键监听器
        dialog.setOnKeyListener((dialog1, keyCode, event) -> {
            // 如果是返回键，并且是按下动作（避免重复事件）
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                // 返回true表示事件被消费，不再传递
                return true;
            }
            return false;
        });

        //自定义的东西
        dialog.getWindow().setContentView(v);
        tvDelete.setOnClickListener(v1 -> dialog.dismiss());
        tvName.setText("为了方便您的使用，请开启权限。");
        tvSave.setOnClickListener(view -> {
            ka();
            dialog.dismiss();
        });
    }

    private void initView() {
        stationList = new ArrayList<>();
        recyclerData = findViewById(R.id.recyclerData);
        recyclerData.setLayoutManager(new LinearLayoutManager(mContext));
        homeAdapter = new HomeAdapter(mContext, stationList);
        recyclerData.setAdapter(homeAdapter);

        strSelect = SPUtils.get(mContext, "strSelect", "") + "";
        pwsSelect = SPUtils.get(mContext, "pwsSelect", "") + "";

        homeAdapter.setOnItemClickListener((position, string) -> {
            stationList.get(0).setType(position);
            strSelect = string.getName();
            pwsSelect = string.getPid();

            SPUtils.put(mContext, "strSelect", strSelect);
            SPUtils.put(mContext, "pwsSelect", pwsSelect);
        });

        findViewById(R.id.lyXitong).setOnClickListener(v -> {
            if (StringUtils.isFastClick()) {
                return;
            }
            if (StringUtils.isEmpty(strSelect)) {
                ToastUtil.showNew(mContext, "请选择工务段");
                return;
            }

            startActivity(new Intent(mContext, MainActivity.class));
            finish();
        });
    }

    private void initData() {
        ChooseBean chooseBean2 = new ChooseBean();
        chooseBean2.setName("西安工务段");
        chooseBean2.setPid("pws001");
        ChooseBean chooseBean3 = new ChooseBean();
        chooseBean3.setName("宝鸡工务段");
        chooseBean3.setPid("pws002");
        ChooseBean chooseBean4 = new ChooseBean();
        chooseBean4.setName("安康工务段");
        chooseBean4.setPid("pws003");
        ChooseBean chooseBean5 = new ChooseBean();
        chooseBean5.setName("汉中工务段");
        chooseBean5.setPid("pws004");
        ChooseBean chooseBean6 = new ChooseBean();
        chooseBean6.setName("阎良工务段");
        chooseBean6.setPid("pws005");
        ChooseBean chooseBean7 = new ChooseBean();
        chooseBean7.setName("延安工务段");
        chooseBean7.setPid("pws006");
        ChooseBean chooseBean8 = new ChooseBean();
        chooseBean8.setName("绥德工电段");
        chooseBean8.setPid("pws008");
        ChooseBean chooseBean9 = new ChooseBean();
        chooseBean9.setName("西安高铁基础设施段");
        chooseBean9.setPid("pws009");
        ChooseBean chooseBean10 = new ChooseBean();
        chooseBean10.setName("延安运营维修段");
        chooseBean10.setPid("pws010");

        stationList.add(chooseBean2);
        stationList.add(chooseBean3);
        stationList.add(chooseBean4);
        stationList.add(chooseBean5);
        stationList.add(chooseBean6);
        stationList.add(chooseBean7);
        stationList.add(chooseBean8);
        stationList.add(chooseBean9);
        stationList.add(chooseBean10);

        homeAdapter.setData(stationList);
    }
}
